package org.jinyan.wechat.service;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.jinyan.led.model.ClientSocket;
import org.jinyan.led.model.Device;
import org.jinyan.led.model.DeviceInstance;
import org.jinyan.led.service.DeviceService;
import org.jinyan.led.tcp.ClientSocketManager;
import org.jinyan.led.tcp.LedServerSocket;
import org.jinyan.wechat.model.WeatherInfo;
import org.jinyan.wechat.model.WechatMessage;
import org.jinyan.wechat.model.WechatReply;

public class ProcessMessageService {
	private static Logger logger = Logger
			.getLogger(ProcessMessageService.class);
	public static String WEATHER = "天气;weather;Weather";
	public static String OPEN = "OPEN;open;打开";
	public static String CLOSE = "CLOSE;close;关闭";
	public static String SET = "SET;set;设置";
	public static String DEL = "del;DEL;移除";

	public String processMsgText(WechatMessage message) {
		String msgText = message.getContent();// 消息内容
		String openID = message.getFromUserName();
		System.out.println("processMsgText start " + msgText);
		logger.info("processMsgText start request" + msgText);
		String msgContent = "请输入请求信息";
		if (msgText != null
				&& (msgText.contains("#") || msgText.contains("＃")
						|| msgText.contains(";") || msgText.contains("；"))) {
			String a[] = msgText.split("#|＃|;|；");
			if (a.length > 1) {
				String operateStr = a[0];
				if (Arrays.asList(WEATHER.split(";")).contains(operateStr)) {
					String area = a[1];
					msgContent = getWeather(area);
				} else if (Arrays.asList(OPEN.split(";")).contains(operateStr)) {
					String ledName = a[1];
					msgContent = operateDevice(openID, ledName, "OPEN");

				} else if (Arrays.asList(CLOSE.split(";")).contains(operateStr)) {
					String ledName = a[1];
					msgContent = operateDevice(openID, ledName, "CLOSE");
				} else if (Arrays.asList(SET.split(";")).contains(operateStr)
						&& a.length==3) {
					String serialNumber = a[1];
					String alias = a[2];
					msgContent = bindingDevice(openID, serialNumber, alias);
				}else if (Arrays.asList(DEL.split(";")).contains(operateStr)
						&& a.length==2) {
					String alias = a[1];
					msgContent = removeDevice(openID, alias);
				}  
				else {
					msgContent = "关键字不对，" + WechatReply.ERROR_CONTENT;
				}

			} else {
				msgContent = "分隔符不对，" + WechatReply.ERROR_CONTENT;
			}
		} else {
			msgContent = "分隔符不对，" + WechatReply.ERROR_CONTENT;
		}
		System.out.println("processMsgText end msgContent=" + msgContent);
		return msgContent;

	}

	private String removeDevice(String openID, String alias) {
			String result = "";
		DeviceService deviceService = new DeviceService();
		result = deviceService.removeDevice(openID, alias);
		return result;
	}

	private String bindingDevice(String openID, String serialNumber,
			String alias) {
		String result = "";
		DeviceService deviceService = new DeviceService();
		result = deviceService.bindingDevice(openID, serialNumber, alias);
		return result;
	}

	private String operateDevice(String openID, String deviceName, String op) {
		String msgContent = null;
		DeviceService deviceService = new DeviceService();
		DeviceInstance instance = deviceService.getDeviceInstance(openID,
				deviceName);
		if (instance == null) {
			return msgContent = deviceName + " 没有绑定。";
		}
		String serialNumber = instance.getSerialNumber();
		String state = instance.getState();

		// Device led = MyLEDCacheManager.getLedByName(ledName);
		ClientSocket client = ClientSocketManager.getClientSocket(serialNumber);
		
		/*if(op.equals(state)){
			return msgContent = deviceName + "已经";
		}*/
	
	    msgContent = deviceName + LedServerSocket.operateDevice(client, op);
	
		return msgContent;
	}

	private String getWeather(String area) {
		WeatherService ws = new WeatherService();
		WeatherInfo info = ws.getWeather(area);
		if (info != null) {
			return info.toWeChatInfo();
		} else {
			return "对不起，获取" + area + "的天气信息失败";
		}

	}

}
